package com.sj.uabrowser.mvp.main.view.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sj.uabrowser.BuildConfig;
import com.sj.uabrowser.MainActivity;
import com.sj.uabrowser.R;
import com.sj.uabrowser.framework.BaseFragment;
import com.sj.uabrowser.framework.BasePresenter;
import com.sj.uabrowser.mvp.main.bean.MainNewFuncs;
import com.sj.uabrowser.mvp.main.view.act.BrowserAct;
import com.sj.uabrowser.mvp.main.view.adp.FuncsAdp;
import com.sj.uabrowser.net.bean.MainFunc;
import com.sj.uabrowser.web.UiController;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class NavFuncsFragment extends BaseFragment {


    @BindView(R.id.rv_funcs)
    RecyclerView rvFuncs;

    int spanCount = 6;//主功能列数
    @BindView(R.id.tv_point1)
    TextView tvPoint1;
    @BindView(R.id.tv_point2)
    TextView tvPoint2;
    @BindView(R.id.ll_pointer)
    LinearLayout llPointer;
    private ArrayList<MainNewFuncs.AdListBean> mainFuncs;

    private UiController uiController;

    public void setUiController(UiController uiController) {
        this.uiController = uiController;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public int getLayoutID() {
        return R.layout.fragment_nav_funcs;
    }

    @Override
    public void setView() {
        mainFuncs = (ArrayList<MainNewFuncs.AdListBean>) getArguments().getSerializable("datas");
        int total = getArguments().getInt("total");
        if (total == 2) {
            llPointer.setVisibility(View.VISIBLE);
            int index = getArguments().getInt("index");
            if(index == 2){
               tvPoint1.setBackground(act.getResources().getDrawable(R.drawable.circle_empty));
                tvPoint2.setBackground(act.getResources().getDrawable(R.drawable.circle_full));
            }
        }
        if (mainFuncs != null) {
            FuncsAdp funcAdp = new FuncsAdp(R.layout.item_func, mainFuncs);
            funcAdp.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    startIconAnim(view);
                    MainNewFuncs.AdListBean bean = mainFuncs.get(position);
                    if(BuildConfig.UM) {
                        String eventid= "navigation-tanghulu";
                        MobclickAgent.onEvent(act, eventid,bean.getTitle()+"-糖葫芦");
                    }
                    if(uiController!=null){
                        uiController.onWebsiteIconClicked(bean.getUrl());
                    }
//                    Intent intent = new Intent(act, BrowserAct.class);
//                    intent.putExtra("type",BrowserAct.TYPE_FUNC);
//                    intent.putExtra("adListBean",bean);
//                    startActivity(intent);
//                    ((MainActivity)act).getAcitiveTab().getWebView().loadUrl(bean.getUrl());
                }
            });
            if(rvFuncs!=null) {
                rvFuncs.setLayoutManager(new GridLayoutManager(act, spanCount));
                rvFuncs.setAdapter(funcAdp);
            }
        }
    }

    @Override
    public void setData() {

    }

}
